#pragma once

#include <QtGui/QWidget>
#include <QtGui/QTextEdit>
#include <QtGui/QDialog>
#include <QtCore/QVector>
#include <QtCore/QMap>
#include <QtCore/QSet>
#include "MainWindow.h"

class QAction;
class QToolBar;
class QCheckBox;
class WebBrowser;
class QTabWidget;
class QKeyEvent;
class Dictionary;
class QRadioButton;
class QLineEdit;
class QPushButton;

class Phrase 
{
public:
  int m_origStart, m_origEnd;
  int m_start, m_end;
  QString m_orig, m_phrase;
  Phrase(int os, int oe, int s, int e, const QString &o, const QString &ph)
    : m_origStart(os), m_origEnd(oe), m_start(s), m_end(e), m_orig(o), m_phrase(ph)
  {}
  Phrase() {}
  void print() const;
};

class Phrases : public QVector<Phrase>
{
public:
  int findPhrase(int orig_pos) const;
  int findPhrase1(int pos) const;
  static QString parse(const QString& text, const QVector<Dictionary*>& dic, Phrases& p);
};

class PhraseEdit : public QTextEdit
{
  Q_OBJECT
signals:
  void positionChanged(int);
  void positionChanged(const QTextCursor&);
  void gotFocus();
public slots:
  void slot_cursorPositionChanged();
public:
  int m_s, m_e;
  Phrases m_phrase;

  PhraseEdit(QWidget* parent = 0) : QTextEdit(parent), m_s(0), m_e(0) {
    connect(this, SIGNAL(cursorPositionChanged ()), this, SLOT(slot_cursorPositionChanged()));
    this->setTextBackgroundColor(Qt::white);
    this->setTextColor(Qt::black);
  }

  void highlight(int s, int e, int pos, bool background=true);

  void focusInEvent ( QFocusEvent * event );

  QString currentText() const;

  void setOriginalText(const QString& text) {
    this->setTextBackgroundColor(Qt::white);
    this->setTextColor(Qt::black);
    setPlainText(text);
    m_s = m_e = 0;
  }
};

class NewPhraseDialog : public QDialog
{
  Q_OBJECT
public slots:
  void slot_allCapital();
  void slot_numberCapital();
public:
  QPushButton *m_btOK, *m_btCancel;
  QRadioButton *m_chkAllCapital, *m_chkNumberCapital;
  QLineEdit *m_lineNumber, *m_lineOrig, *m_lineHV, *m_lineTR;
  QCheckBox *m_chkSeparateDictionary;
  bool m_chooseHV;

  NewPhraseDialog(QWidget* parent = 0);
  void set_chooseHV(bool chooseHV) { m_chooseHV = chooseHV; }
};

class TextWindow : public QWidget
{
	Q_OBJECT
public:
	TextWindow(const QString& windowName, MainWindow *mainWindow, QWidget *parent = 0);
	~TextWindow();
signals:
  void refreshHighlight(int tr_s, int tr_e, int hv_s, int hv_e, int orig_s, int orig_e);
public slots:
  void slot_origPosChanged(int);
  void slot_trPosChanged(int);
  void slot_hvPosChanged(int);

  void slot_origPosChanged(const QTextCursor&);
  void slot_trPosChanged(const QTextCursor&);
  void slot_hvPosChanged(const QTextCursor&);

  void slot_origFocused();
  void slot_trFocused();
  void slot_hvFocused();

  void slot_addName();
  void slot_addPhrase();
  void slot_addHV();
  void slot_refresh();
  void slot_saveDictionary();
  void slot_loadDictionary();
  void slot_refreshHighlight(int tr_s, int tr_e, int hv_s, int hv_e, int orig_s, int orig_e);
  void slot_fromClipboard();
protected:
  PhraseEdit *m_origText, *m_hvText, *m_trText;
  QAction *m_actAddName, *m_actAddPhrase, *m_actAddHV,
          *m_actRefresh, *m_actSaveDictionary, *m_actLoadDictionary,
          *m_actFromClipboard;
  QToolBar *m_tbDictionary;
  MainWindow *m_mainWindow;

	void createActions();
	void createWidgets();
	void createToolbar();
	void createLayout();
	void connectSignals();
	
	void keyPressEvent(QKeyEvent *event);

public:
  void setOriginalText(const QString& text);
  void hvFocus() { m_trText->setFocus(); }
  void createPhraseDialog(NewPhraseDialog &d, bool chooseHV) const;
  static void get_interval(const QTextCursor& c, int &min, int &max);
};
